package org.fanpeng.deom.design;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * className: Admin
 * Description:
 * date: 2021/9/14 15:01
 *
 * @author fanpeng
 * @version 1.0
 */
public class Admin {

    private int cursorIdx = 0;
    private List<ConfigMemento> mementoList = new ArrayList<>();
    private Map<String, ConfigMemento> mementoMap = new HashMap<>();

    public void append(ConfigMemento configMemento){
        mementoList.add(configMemento);
        mementoMap.put(configMemento.getConfigFile().getVersionNo(), configMemento);
        cursorIdx++;
    }

    public ConfigMemento undo(){
        if (--cursorIdx <= 0) {
            return mementoList.get(0);
        }
        return mementoList.get(cursorIdx);
    }

    public ConfigMemento redo() {
        if (++cursorIdx > mementoList.size()){
            return mementoList.get(mementoList.size() - 1);
        }
        return mementoList.get(cursorIdx);
    }

    public ConfigMemento get(String versionNo){
        return mementoMap.get(versionNo);
    }

}
